package com.hongkou.tvstation.news.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hongkou.tvstation.common.CodeCst;
import com.hongkou.tvstation.common.action.GenericAction;
import com.hongkou.tvstation.news.dao.NewsDao;
import com.hongkou.tvstation.news.vo.NewsVO;

public class NewsManagementAction extends GenericAction {
	private NewsDao newsDao;

	public NewsDao getNewsDao() {
		return newsDao;
	}

	public void setNewsDao(NewsDao newsDao) {
		this.newsDao = newsDao;
	}

	@Override
	public ActionForward process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		NewsManagementForm newsManagementForm = (NewsManagementForm) form;
		NewsVO queryVO = new NewsVO();
		BeanUtils.copyProperties(queryVO, newsManagementForm);
		String actionType = request
				.getParameter(CodeCst.PARAM__ACTION_ACTION_TYPE);
		String forward = CodeCst.FORWARD__NEWS_PAGE;
		if (actionType == null || "".equals(actionType)) {
			request.setAttribute("news", newsDao.loadAllNews(queryVO));
			request.setAttribute("pageVO", queryVO.getPageVO());
			forward = CodeCst.FORWARD__NEWS_PAGE;
		} else if ("displayNews".equals(actionType)) {
			Long newsId = Long.valueOf(newsManagementForm.getNewsId());
			NewsVO newsVO = newsDao.getNews(newsId);
			request.setAttribute("news", newsVO);
			forward = "newsDisplay";
		} else if ("showNews".equals(actionType)) {
			request.setAttribute("news", newsDao.loadAllNews(queryVO));
			request.setAttribute("pageVO", queryVO.getPageVO());
			forward = CodeCst.FORWARD__NEWS_LIST_PAGE;
		}
		return mapping.findForward(forward);
	}
}
